<?php
/**
 * Created by JetBrains PhpStorm.
 * User: GMC02
 * Date: 10/9/13
 * Time: 5:22 PM
 * To change this template use File | Settings | File Templates.
 */
class HybridAuthIdentity extends CUserIdentity {
    const VERSION = '2.1.2';

    /**
     *
     * @var Hybrid_Auth
     */
    public $hybridAuth;

    /**
     *
     * @var Hybrid_Provider_Adapter
     */
    public $adapter;

    /**
     *
     * @var Hybrid_User_Profile
     */
    public $userProfile;

    public $allowedProviders = array('google', 'facebook', 'linkedin', 'yahoo', 'live',);

    protected $config;

    function __construct() {
        $path = Yii::getPathOfAlias('ext.hybridauth');
        require_once $path . '/Hybrid/Auth.php'; //path to the Auth php file within HybridAuth folder


        $this->config = array(
            "base_url" => Yii::app()->getBaseUrl(true) . '/user/socialLogin',

            "providers" => array(
                "Google" => array(
                    "enabled" => false,
                    "keys" => array(
                        "id" => "google client id",
                        "secret" => "google secret",
                    ),
                    "scope" => "https://www.googleapis.com/auth/userinfo.profile " . "https://www.googleapis.com/auth/userinfo.email",
                    "access_type" => "online",
                ),
                "Facebook" => array(
                    "enabled" => true,
                    "keys" => array(
                        "id" => "537495016299869",
                        "secret" => "55a79ee3757e92f075b2c368a167d954",
                    ),
                    "scope" => "email"
                ),
                "Live" => array(
                    "enabled" => false,
                    "keys" => array(
                        "id" => "windows client id",
                        "secret" => "Windows Live secret",
                    ),
                    "scope" => "email"
                ),
                "Yahoo" => array(
                    "enabled" => false,
                    "keys" => array(
                        "key" => "yahoo client id",
                        "secret" => "yahoo secret",
                    ),
                ),
                "LinkedIn" => array(
                    "enabled" => false,
                    "keys" => array(
                        "key" => "linkedin client id",
                        "secret" => "linkedin secret",
                    ),
                ),
            ),

            "debug_mode" => true,

            // to enable logging, set 'debug_mode' to true, then provide here a path of a writable file
            "debug_file" => Yii::getPathOfAlias('application.logs') . '\hybrid_auth.log',
        );

        $this->hybridAuth = new Hybrid_Auth($this->config);
    }

    /**
     *
     * @param string $provider
     * @return bool
     */
    public function validateProviderName($provider) {
        if (!is_string($provider))
            return false;
        if (!in_array($provider, $this->allowedProviders))
            return false;

        return true;
    }

    public function login() {
        $this->username = $this->userProfile->email; //CUserIdentity
        $this->setState('email', $this->username);
        Yii::app()->user->login($this, 0);
    }

    public function authenticate() {
        return true;
    }

}